﻿Imports System.Text.RegularExpressions

Public Class FormCustomerEditor

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click

        If txtCustomerName.Text = "" Then
            MsgBox("Please Input Name!")
            txtCustomerName.Focus()
            Exit Sub
        End If

        If txtPhone.Text = "" Then
            MsgBox("Please Input Phone!")
            txtPhone.Focus()
            Exit Sub
        End If

        Dim d = "^[0]{1}[0-9]{2} [0-9]{3} [0-9]{3,4}$"
        If Regex.IsMatch(txtPhone.Text, d) = False Then
            MsgBox("Please input a correct phone format!")
            txtPhone.Focus()
            Exit Sub
        End If

        Dim p = "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$"
        If Regex.IsMatch(txtEmail.Text, p) = False Then
            MsgBox("Please input a correct email format!")
            txtEmail.Focus()
            Exit Sub
        End If

        Sql.Parameters("@CUSTOMER_NAME") = txtCustomerName.Text
        Sql.Parameters("@PHONE") = txtPhone.Text
        Sql.Parameters("@EMAIL") = txtEmail.Text
        Sql.Parameters("@ADDRESS") = txtAddress.Text

        If Me.PictureBox1.Image Is Nothing Then
            Sql.Parameters("@PHOTO") = DBNull.Value
        Else
            Dim m As New System.IO.MemoryStream()
            Me.PictureBox1.Image.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg)
            Dim bytes As Byte() = m.GetBuffer()
            Sql.Parameters("@PHOTO") = bytes
        End If

        If txtCustomerId.Enabled Then
            Sql.ExecuteNonQuery("INSERT INTO Customer VALUES(@CUSTOMER_NAME,@PHONE,@EMAIL,@ADDRESS,1,@PHOTO)")
        Else
            Sql.Parameters("@CUSTOMER_ID") = Val(txtCustomerId.Text)
            Sql.ExecuteNonQuery("UPDATE Customer SET CustomerName=@CUSTOMER_NAME,Phone=@PHONE,Email=@EMAIL,Address=@ADDRESS,Photo=@PHOTO WHERE CustomerId = @CUSTOMER_ID")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim diag As OpenFileDialog = New OpenFileDialog()
        diag.Filter = "*.JPG|*.JPG|*.PNG|*.PNG|*.GIF|*.GIF|*.TIF|*.TIF|*.BMP|*.BMP"

        If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then

            Me.PictureBox1.Image = Image.FromFile(diag.FileName)
        End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.PictureBox1.Image = Nothing

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'Dim diag As SimpleCam.FormCamera
        'diag = New SimpleCam.FormCamera(True, False, False)
        'If diag.ShowDialog() = Windows.Forms.DialogResult.OK Then
        '    PictureBox1.Image = diag.ClippedImage
        'End If

    End Sub
End Class